using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces;

namespace SheLife.Application.Services
{
    /// <summary>
    /// 健康记录服务实现
    /// </summary>
    public class HealthRecordService : IHealthRecordService
    {
        private readonly IRepository<HealthRecord> _healthRecordRepository;
        private readonly IRepository<User> _userRepository;
        private readonly IUnitOfWork _unitOfWork;

        public HealthRecordService(
            IRepository<HealthRecord> healthRecordRepository,
            IRepository<User> userRepository,
            IUnitOfWork unitOfWork)
        {
            _healthRecordRepository = healthRecordRepository;
            _userRepository = userRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取健康记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns>健康记录实体</returns>
        public async Task<HealthRecord?> GetHealthRecordByIdAsync(Guid id)
        {
            return await _healthRecordRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取用户的所有健康记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>健康记录列表</returns>
        public async Task<IEnumerable<HealthRecord>> GetHealthRecordsByUserIdAsync(Guid userId)
        {
            var allRecords = await _healthRecordRepository.GetAllAsync();
            return allRecords.Where(r => r.UserId == userId).OrderByDescending(r => r.RecordDate);
        }

        /// <summary>
        /// 获取用户指定类型的健康记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="recordType">记录类型</param>
        /// <returns>健康记录列表</returns>
        public async Task<IEnumerable<HealthRecord>> GetHealthRecordsByTypeAsync(Guid userId, string recordType)
        {
            var allRecords = await _healthRecordRepository.GetAllAsync();
            return allRecords.Where(r => r.UserId == userId && r.RecordType == recordType)
                             .OrderByDescending(r => r.RecordDate);
        }

        /// <summary>
        /// 获取用户指定日期范围的健康记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>健康记录列表</returns>
        public async Task<IEnumerable<HealthRecord>> GetHealthRecordsByDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            var allRecords = await _healthRecordRepository.GetAllAsync();
            return allRecords.Where(r => r.UserId == userId && r.RecordDate >= startDate && r.RecordDate <= endDate)
                             .OrderBy(r => r.RecordDate);
        }

        /// <summary>
        /// 创建健康记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="recordType">记录类型</param>
        /// <param name="recordDate">记录日期</param>
        /// <param name="value">记录值</param>
        /// <param name="notes">备注</param>
        /// <returns>健康记录实体</returns>
        public async Task<HealthRecord> CreateHealthRecordAsync(Guid userId, string recordType, DateTime recordDate, string value, string? notes = null)
        {
            // 验证用户是否存在
            var user = await _userRepository.GetByIdAsync(userId);
            if (user == null || user.IsDeleted)
            {
                throw new ArgumentException("用户不存在");
            }

            // 创建健康记录
            var healthRecord = HealthRecord.Create(userId, recordType, recordDate, value, notes);

            await _healthRecordRepository.AddAsync(healthRecord);
            await _unitOfWork.SaveChangesAsync();

            return healthRecord;
        }

        /// <summary>
        /// 更新健康记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <param name="value">记录值</param>
        /// <param name="notes">备注</param>
        /// <returns>健康记录实体</returns>
        public async Task<HealthRecord?> UpdateHealthRecordAsync(Guid id, string value, string? notes = null)
        {
            var healthRecord = await _healthRecordRepository.GetByIdAsync(id);
            if (healthRecord == null)
            {
                return null;
            }

            healthRecord.Update(value, notes);
            await _healthRecordRepository.UpdateAsync(healthRecord);
            await _unitOfWork.SaveChangesAsync();

            return healthRecord;
        }

        /// <summary>
        /// 删除健康记录
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteHealthRecordAsync(Guid id)
        {
            var healthRecord = await _healthRecordRepository.GetByIdAsync(id);
            if (healthRecord == null)
            {
                return false;
            }

            await _healthRecordRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();

            return true;
        }
    }
}